<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

//require APPPATH."third_party/MX/Controller.php";

/**
 * Abstract Class of all application controllers
 *
 * @author	kimon.web.design - Application Framework
 * @package 	CMS
 * @subpackage 	Public_Controller
 * @category 	Framework
 * @copyright	Copyright (c) 2011, kimon.web.design
*/
class MY_Controller extends Controller {

    protected $debug_buffer = '';

    /**
     * the template name that is used as Master Template
     * @var string
     */
    protected $template_name = 'default';

    /**
     * flags if the controller needs authentication
     * @var boolean 
     */
    protected $needs_authentication = FALSE;


    protected function debug($str) {
        $this->debug_buffer .= $str . '<br/>';
    }

    /**
     * Constructor method
     * @access public
     * @return void
     */
    public function  __construct() {
        parent::__construct();

        // check authentication
        $this->__defineAuthentication();
        $this->checkLogin();

        // set the appropriate template for the controller
        $this->__defineTemplateView();
        $this->template->set_template($this->template_name);

        $this->debugUri();
    }

    function debugUri() {
        $this->debug( 'uri_string:' . $this->uri->uri_string() );
        $this->debug(  'ruri_string:' . $this->uri->ruri_string() );
        $this->debug(  '<br/>' );

        $i = 1;
        $rs = $this->uri->rsegment_array();
        foreach ($rs as $seg)
        {
            $this->debug(  $i . '. ' . $seg );
            $i++;
        }
    }

    /**
     * set the Master Template of the Controller
     *
     * @access public
     * @return void
     */
    protected function __defineTemplateView()
    {
        $this->template_name = 'default';
    }

    /**
     * set if the Controller needs Authentication
     *
     * @access public
     * @return void
     */
    protected function __defineAuthentication()
    {
        $this->needs_authentication = FALSE;
    }

    /**
     * @access private
     * @return void
     */
    private function checkLogin()
    {
        if ($this->needs_authentication) {
            if (!$this->ion_auth->logged_in())
            {
                redirect('auth/login');
            }
        }
    }

    protected function render() {
        
        $this->template->write('debug', $this->debug_buffer);
        $this->template->render();
    }

    protected function _show_404_page($msg='')
    {
        echo $this->debug_buffer;

        show_error('404 ' . $msg);
    }

}

/* End of file MY_Controller.php */
/* Location: ./application/core/MY_Controller.php */